package com.yj.suncity.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.yj.suncity.R;
import com.yj.suncity.bean.AdvGoodsBean;
import com.yj.suncity.utis.GoodsView;
import com.yj.suncity.utis.GoodsView.GoodsViewListener;

public class GoodsListAdapter extends BaseAdapter implements GoodsViewListener{
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<AdvGoodsBean> mList;
	private ProductItemListener mListener;
	
	private float scale;
	private int width;

	public GoodsListAdapter(Context context, ArrayList<AdvGoodsBean> list, int scwidth,ProductItemListener listener) {
		this.mList = list;
		this.mInflater = LayoutInflater.from(context);
		this.mContext = context;
		this.width = scwidth;
		this.mListener = listener;
		scale = mContext.getResources().getDisplayMetrics().density;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("ViewHolder")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		// if(convertView == null){
		GoodsView goodsView = new GoodsView(mContext);
		goodsView.setGoodsViewListener(this);
		convertView = mInflater.inflate(R.layout.product_item, null);

		final ViewHolder holder = new ViewHolder();
		holder.liftLayout = (RelativeLayout) convertView.findViewById(R.id.product_list_left_item);
		holder.rightLayout = (RelativeLayout) convertView.findViewById(R.id.product_list_right_item);
		holder.layout = (RelativeLayout)convertView.findViewById(R.id.goods_list_relative);
		
		convertView.setTag(holder);
		
		if(2 * position < mList.size()){
			holder.layout.setVisibility(View.VISIBLE);
			LinearLayout item = goodsView.getLinearLayout(mList.get(2 * position),2 * position);
			holder.liftLayout.addView(item);
		}else{
			holder.layout.setVisibility(View.GONE);
		}
		
		if(2 * position + 1 < mList.size()){
			holder.layout.setVisibility(View.VISIBLE);
			LinearLayout item = goodsView.getLinearLayout(mList.get(2 * position + 1),2 * position + 1);
			holder.rightLayout.addView(item);
		}else{
		}
		return convertView;
	}

	public class ViewHolder {
		public RelativeLayout liftLayout;
		public RelativeLayout rightLayout;
		public RelativeLayout layout;
	}

	@Override
	public void itemClick(int position) {
		// TODO Auto-generated method stub
		mListener.productItemClick(position);
	}
	public interface ProductItemListener{
		public void productItemClick(int position);
	}
}
